package com.guigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.guigu.constant.MessageConstant;
import com.guigu.entity.PageResult;
import com.guigu.entity.QueryPageBean;
import com.guigu.entity.Result;
import com.guigu.pojo.Habitus;
import com.guigu.service.HabitusService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author 陈小婷
 * @date 2021/4/22
 */
@RestController
@RequestMapping("/habitus")
public class HabitusController {
    @Reference
    private HabitusService habitusService;

    @RequestMapping("/add")
    public Result add(@RequestBody Habitus habitus) throws IOException {
        try {
            habitusService.add(habitus);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_HABITUS_FAIL);
        }
        return new Result(true,MessageConstant.ADD_HABITUS_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            habitusService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_HABITUS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_HABITUS_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Habitus habitus){
        try {
            habitusService.edit(habitus);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_HABITUS_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_HABITUS_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Habitus habitus = habitusService.findById(id);
            return new Result(true,MessageConstant.QUERY_HABITUS_SUCCESS,habitus);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_HABITUS_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        List<Habitus> all = habitusService.findAll();
        if (all != null && all.size()>0){
            Result result = new Result(true, MessageConstant.QUERY_HABITUS_SUCCESS);
            result.setData(all);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_HABITUS_FAIL);
    }

    /**
     *分页查询
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=habitusService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }


}
